/*
 * MaterialBibliografico.java
 *
 * Created on 19 de mayo de 2007, 11:34 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package everest3.materialBibliografico;

import tablasYBasesdeDatos.Conexion;

import java.util.Vector; 
import javax.swing.JDialog; 
import javax.swing.JTable;

/**
 *
 * @author jose
 */
public class MaterialBibliografico {
    
    /** Creates a new instance of MaterialBibliografico */
    public MaterialBibliografico() {
    }
    public static int actualizarMaterialBibliografico(Object[] campos, int tipo, javax.swing.JDialog parent)
    {
       Conexion con = new Conexion();
       String sql =""; 
       String msg1 ="", msg2="";
        con.conectar("everest","postgres","root");
        
        // <editor-fold defaultstate="collapsed" desc=" columnas materialbibliografico ">
        /*
         *CREATE TABLE materialbibliografico
                    (
                      areacodigo smallint NOT NULL,+ 0          
                      matcons smallint NOT NULL,+ 1
                      matnom character varying NOT NULL,+ 2
                      matautor character varying,+ 3
                      matedit character varying(100),+ 4
                      matvolu character varying,+ 5
                      matedic character varying,+ 6
                      mattomo character varying,+ 7
                      matannopub smallint,+ 8
                      matcodbar character varying(10),+ 9
                      matsigtop character varying(20),+ 10
                      matestado smallint,+ 11
                      matfeching date,+ 12
                      matmodoadq smallint,+ 13
                      matobs character varying,+ 14 
                      matdsp smallint,+ 15
                      catecod smallint, 16
                      CONSTRAINT materialbibliografico_pkey PRIMARY KEY (areacodigo, matcons)
                    ) 
                    WITHOUT OIDS;
                    ALTER TABLE materialbibliografico OWNER TO postgres;

         */
        //</editor-fold>
        
        switch(tipo)          
        {
            case 0: {
                        sql = "INSERT INTO materialbibliografico VALUES("+         
                        campos[0] +","+campos[1]+",'"+campos[2]+"','"+campos[3]+"','"+campos[4]+"','"+
                        campos[5]+"','"+campos[6]+"','"+campos[7]+"',"+campos[8]+",'"+campos[9]+"','"+
                        campos[10]+"',"+campos[12]+",'"+campos[15]+"',"+campos[14]+",'"+campos[11]+"',"+
                        campos[13]+","+campos[16]+")";
                        msg1 = "insert\u00f3";
                        msg2 = "insertar";
                    }
                
                    break;
                   
            case 1: 
                    {
                        sql = "UPDATE materialbibliografico "+
                              "SET matnom ='"+campos[2]+"',matautor='"+campos[3]+"',matedit='"+campos[4]+"', "+
                              "matvolu='"+campos[5]+"', matedic='"+campos[6]+"',mattomo='"+campos[7]+"',matannopub="+campos[8]+
                              ",matcodbar='"+campos[9]+"',matsigtop='"+campos[10]+"',matestado="+campos[12]+",matfeching='"+campos[15]+
                              "',matmodoadq="+campos[14]+",matobs='"+campos[11]+"',matdsp="+campos[16]+",catecod="+campos[13]+" "+
                              "WHERE areacodigo="+campos[0]+" AND matcons="+campos[1];
                        msg1 = "modific\u00f3";
                        msg2 = "modificar";
                        
                    }
        }
                
        int res = con.actualizar(sql);
        if(res == 0)
	{
		javax.swing.JOptionPane.showMessageDialog(parent, "Error al "+msg2+" en la base de datos." +
                                                     "\nEl material bibliogr\u00e1fico con el c\u00f3digo \""
                                                    +campos[0]+" - "+campos[1]+"\" ya existe.",
                                                       "Error",javax.swing.JOptionPane.WARNING_MESSAGE);	
	}
	else
	{
		javax.swing.JOptionPane.showMessageDialog(parent, "Se "+msg1+" correctamente "+
                                                    " el material bibliogr\u00e1fico con el c\u00f3digo \""
                                                    +campos[0]+" - "+campos[1]+"\".");	
	}
	
        con.hacerCommit();
	con.desconectar();        
       
        return res;
    }
    
     public java.util.Vector consultaIndividual(String area, String cons)
    {
        String sql ="SELECT * FROM materialbibliografico where areacodigo = "
                    +area+" AND matcons = "+cons;
        
        tablasYBasesdeDatos.JDBCAdapter dataBase = new tablasYBasesdeDatos.JDBCAdapter(
                    "jdbc:postgresql://localhost:5432/everest",
                    "org.postgresql.Driver","postgres","root");
        
        java.util.Vector resultadoBusqueda = dataBase.ConsultaIndividual(sql);
        
         try
        {
            dataBase.close();
        }
        catch(java.sql.SQLException sqle){System.err.println(sqle.getMessage());}
        
        return resultadoBusqueda;
    }
      public javax.swing.JTable consultarMaterialBibliografico(String campo)
    {
       
        
        
        tablasYBasesdeDatos.JDBCAdapter ConsultaMaterialBD = new tablasYBasesdeDatos.JDBCAdapter(
                    "jdbc:postgresql://localhost:5432/everest",
                    "org.postgresql.Driver","postgres","root");
        String nombresColumnas[]={"Area", "Consecutivo", "Nombre del material bibliogr\u00e1fico","Disponible"};
 
        ConsultaMaterialBD.asignarNombreColumnas(nombresColumnas);

        
        ConsultaMaterialBD.consultaConFormatoMaterialBibliog("SELECT " +
                                                                "areacodigo," +
                                                                "matcons," +
                                                                "matnom," +
                                                                "matdsp " +
                                                            "from " +
                                                                "materialbibliografico " +
                                                            "where " +
                                                                "matnom like '%"+campo+"%' OR " +
                                                                "matautor like'%"+campo+"%' or " +
                                                                "matedit like'%"+campo+"%' " +
                                                            "ORDER BY matnom ASC");
 
        tablasYBasesdeDatos.TableSorter sorter = new tablasYBasesdeDatos.TableSorter();

        sorter.setModel(ConsultaMaterialBD);

        // Reasigna a la tabla un TableModel con los resultados de la consulta
        javax.swing.JTable resultadoConsulta = new javax.swing.JTable(sorter); 

        //agrega al scrollbar la tabla con la informacion resultante de la consulta
        resultadoConsulta.setAutoCreateRowSorter(true);     		        
        try
        {
            ConsultaMaterialBD.close();
        }
        catch(java.sql.SQLException sqle){System.err.println(sqle.getMessage());}
        return resultadoConsulta;
      }
    
    
}
